home *** CD-ROM | disk | FTP | other *** search
/ Amiga News 95 / Amiga News 95.iso / dpat / dpat48 / dearch / sources / dearch.c < prev    next >
C/C++ Source or Header  |  1993-11-14  |  12KB  |  380 lines

  1. /*
  2.  * DeArch.c
  3.  * Version 1.0
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <exec/lists.h>
  9. #include <dos/rdargs.h>
  10. #include <libraries/locale.h>
  11. #include <libraries/commodities.h>
  12. #include <libraries/amigaguide.h>
  13. #include <intuition/intuition.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/locale_protos.h>
  18. #include <clib/amigaguide_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/commodities_protos.h>
  21.  
  22. #ifdef __SASC
  23. #include <pragmas/exec_pragmas.h>
  24. #include <pragmas/dos_pragmas.h>
  25. #include <pragmas/locale_pragmas.h>
  26. #include <pragmas/amigaguide_pragmas.h>
  27. #include <pragmas/intuition_pragmas.h>
  28. #include <pragmas/commodities_pragmas.h>
  29. #endif
  30.  
  31. #include <stdlib.h>
  32.  
  33. #define CATCOMP_NUMBERS
  34.  
  35. #include "Globals.h"
  36. #include "CrunchPrefs.h"
  37. #include "local_dearch.h"
  38. #include "dearchguide.h"
  39.  
  40. #include "dearch_strings.h"
  41.  
  42. #define EV_HOTKEY       1L
  43.  
  44. #define TEMPLATE        "h=?/S"
  45. #define OPT_HELP        0
  46. #define OPT_COUNT       1
  47.  
  48. static UBYTE *VersTag = "\0$VER: DeArch V1.0 (17.10.93)";
  49.  
  50. struct Library *LocaleBase, *AmigaGuideBase, *CxBase, *IconBase;
  51. struct LocaleInfo li; 
  52. static struct MsgPort *cxport;
  53. static CxObj *broker, *filter;
  54.  
  55. static struct NewBroker newbroker =
  56. {
  57.         NB_VERSION,
  58.         "DeArch",
  59.         NULL,
  60.         NULL,
  61.         0,
  62.         COF_SHOW_HIDE, 0, 0, 0
  63. };
  64.  
  65. static struct EasyStruct ErrorMain = {
  66.          sizeof(struct EasyStruct), 0, "DeArch",
  67.          "%s", "OK"
  68. };
  69.  
  70. static LONG opts[OPT_COUNT];
  71. static struct RDArgs *rdargs;
  72. UBYTE *hotkey;
  73. BOOL done = FALSE;
  74.  
  75. extern struct Library *IntuitionBase, *DOSBase;
  76. extern struct List myList;
  77. extern struct Window *ArchiveurWnd,*ArchiWnd, *PrefsWnd;
  78.  
  79. #ifdef __SASC
  80. extern __stdargs CxObj *HotKey(STRPTR, struct MsgPort *, LONG); 
  81. extern __stdargs UBYTE **ArgArrayInit(LONG, UBYTE **);
  82. extern __stdargs LONG ArgInt(UBYTE **, STRPTR, LONG);
  83. extern __stdargs STRPTR ArgString(UBYTE **, STRPTR, STRPTR);
  84. extern __stdargs void ArgArrayDone(void);
  85. #endif
  86.  
  87. BOOL OpenLibs(void);
  88. void CloseLibs(void);
  89. void ProcessMsg(void);
  90. void InitListView(void);
  91. void KillListView(void);
  92. void CloseAllWnd(void);
  93. void CleanExit(void);
  94. void main(int, char **);
  95.  
  96. void main(int argc, char **argv)
  97. {
  98.          UBYTE **ttypes;
  99.                           
  100.          rdargs = ReadArgs(TEMPLATE, opts, NULL); 
  101.          
  102.          if (IconBase = OpenLibrary("icon.library", 37))
  103.          { 
  104.             ttypes = ArgArrayInit(argc, argv);
  105.             newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  106.             hotkey = ArgString(ttypes, "CX_POPKEY", "control lalt d"); 
  107.          }
  108.                
  109.          
  110.          if (LocaleBase = OpenLibrary("locale.library", 38))
  111.          {
  112.             li.li_LocaleBase = LocaleBase;
  113.             li.li_Catalog = OpenCatalogA(NULL, "dearch.catalog", NULL); 
  114.    
  115.             if (FindPort("Dearch-Port"))
  116.             {
  117.                 EasyRequest(NULL, &ErrorMain, NULL,
  118.                                        GetString(&li, MSG_ACTIVE));
  119.                                        
  120.                 CloseCatalog(li.li_Catalog);
  121.                 CloseLibrary(LocaleBase);
  122.                 if (IconBase)
  123.                 {
  124.                         ArgArrayDone();
  125.                         CloseLibrary(IconBase);
  126.                 }
  127.                 if (rdargs)
  128.                   FreeArgs(rdargs);  
  129.                 exit(0);                                         
  130.             }
  131.   
  132.             if (OpenLibs())
  133.             {    
  134.                 nag.nag_BaseName = "DeArch";
  135.                 nag.nag_Name = "DeArch.guide";
  136.                 
  137.                 if (opts[OPT_HELP])                
  138.                    if (aguide = OpenAmigaGuideA(&nag, NULL))
  139.                    {
  140.                          CloseAmigaGuide(aguide);   
  141.                          aguide = NULL;
  142.                    }
  143.                                                   
  144.                 InitAmigaGuide();
  145.                         
  146.                 if (SetupScreen())
  147.                 { 
  148.                    InitListView(); 
  149.                    LoadPrefs();  
  150.                    
  151.                    if (OpenArchiveurWindow())
  152.                    {     
  153.                      if (cxport = CreateMsgPort())                     
  154.                      {
  155.                        cxport->mp_Node.ln_Pri = 0;
  156.                        cxport->mp_Node.ln_Name = "Dearch-Port"; 
  157.                        AddPort(cxport);
  158.                        
  159.                        newbroker.nb_Port = cxport;
  160.                        newbroker.nb_Title = GetString(&li, MSG_COPYRIGHT);
  161.                        newbroker.nb_Descr = GetString(&li, MSG_USE);
  162.                     
  163.                        if (broker = CxBroker(&newbroker, NULL))
  164.                        {
  165.                          if (filter = HotKey(hotkey, cxport, EV_HOTKEY))
  166.                          {
  167.                            AttachCxObj(broker, filter);
  168.                            if (! CxObjError(filter))
  169.                            {
  170.                              ActivateCxObj(broker, 1);
  171.                              ProcessMsg();                                                                 
  172.                            }
  173.                            else
  174.                               EasyRequest(NULL, &ErrorMain, NULL,
  175.                                  GetString(&li, ERROR_MAINCOM));      
  176.                          }
  177.                          else
  178.                              EasyRequest(NULL, &ErrorMain, NULL,
  179.                                 GetString(&li, ERROR_MAINHOTKEY));                              
  180.                        }
  181.                        else
  182.                            EasyRequest(NULL, &ErrorMain, NULL,
  183.                                 GetString(&li, ERROR_MAINBROKER));                                             
  184.                      }
  185.                      else
  186.                        EasyRequest(NULL, &ErrorMain, NULL,
  187.                            GetString(&li, ERROR_MAINPORT));     
  188.                    }
  189.                    else
  190.                       EasyRequest(NULL, &ErrorMain, NULL,
  191.                          GetString(&li, ERROR_MAINWIND));                         
  192.                 }
  193.                 else
  194.                    EasyRequest(NULL, &ErrorMain, NULL,
  195.                       GetString(&li, ERROR_MAINSETUP));                                                                
  196.             }
  197.             else
  198.                EasyRequest(NULL, &ErrorMain, NULL,
  199.                    GetString(&li, ERROR_MAINLIB));       
  200.          }              
  201.          CleanExit();
  202. }
  203.                 
  204. void ProcessMsg(void)
  205. {  
  206.        ULONG msgid, msgtype, signals;
  207.        CxMsg *msg;      
  208.        ULONG wndsigflag = 1 << ArchiveurWnd->UserPort->mp_SigBit; 
  209.        ULONG cxsigflag = 1 << cxport->mp_SigBit;
  210.        ULONG archisigflag, prefssigflag;
  211.                             
  212.        while (!done)
  213.        {
  214.           if (ArchiWnd)
  215.             archisigflag = 1 << ArchiWnd->UserPort->mp_SigBit;
  216.           else
  217.             archisigflag = 0;
  218.            
  219.           if (PrefsWnd)      
  220.              prefssigflag = 1 << PrefsWnd->UserPort->mp_SigBit;
  221.            else
  222.             prefssigflag = 0;
  223.  
  224.           signals = Wait(wndsigflag | archisigflag | prefssigflag | cxsigflag | amsig);
  225.           
  226.           if (signals & wndsigflag)  
  227.                 HandleMainIDCMP();
  228.           
  229.           if (signals & archisigflag)
  230.                 HandleArchiIDCMP();
  231.           
  232.           if (signals & prefssigflag)
  233.                  HandlePrefsIDCMP();
  234.           
  235.           if (signals & amsig)
  236.                 HandleAmigaguide();
  237.                                    
  238.           if (signals & cxsigflag)
  239.           {
  240.                 while (msg = (CxMsg *)GetMsg(cxport))
  241.                 {
  242.                    msgid = CxMsgID(msg);
  243.                    msgtype = CxMsgType(msg);
  244.                    ReplyMsg((struct Message *)msg);
  245.                    
  246.                    switch (msgtype)
  247.                    {
  248.                         case CXM_IEVENT:
  249.                            if (ArchiveurWnd == NULL)
  250.                            {
  251.                               OpenArchiveurWindow();
  252.                               wndsigflag = 1 << ArchiveurWnd->UserPort->mp_SigBit;
  253.                            }   
  254.                            break;
  255.  
  256.                         case CXM_COMMAND:
  257.                           switch(msgid)
  258.                           {
  259.                              case CXCMD_DISABLE:
  260.                                 ActivateCxObj(broker, 0);
  261.                                 break;
  262.                                 
  263.                              case CXCMD_ENABLE:
  264.                                 ActivateCxObj(broker, 1);
  265.                                 break;
  266.                                 
  267.                              case CXCMD_APPEAR:
  268.                                if (ArchiveurWnd == NULL)
  269.                                {
  270.                                  OpenArchiveurWindow();
  271.                                  wndsigflag = 1 << ArchiveurWnd->UserPort->mp_SigBit;
  272.                                }                                 
  273.                                break;
  274.                                
  275.                              case CXCMD_DISAPPEAR:
  276.                                  CloseAllWnd();
  277.                                  wndsigflag = archisigflag = prefssigflag = 0;
  278.                                  break;  
  279.                                    
  280.                              case CXCMD_KILL:
  281.                                 done = TRUE;                           
  282.                                 break;
  283.                                          
  284.                           }
  285.                    }                       
  286.                 }                        
  287.           } 
  288.        }    
  289. }                
  290.  
  291. void CloseAllWnd(void)
  292. {
  293.         if (ArchiveurWnd)
  294.            CloseArchiveurWindow();
  295.         if (ArchiWnd)
  296.            CloseArchiWindow();
  297.         if (PrefsWnd)
  298.            ClosePrefsWindow();              
  299. }
  300.         
  301. void InitListView(void)
  302. {
  303.         NewList(&myList);
  304. }
  305.  
  306. void KillListView(void)
  307. {
  308.         struct crunch *node, *nextnode;
  309.         
  310.         node = (struct crunch *)(myList.lh_Head);
  311.         while (nextnode = (struct crunch *)(node->cr_Node.ln_Succ))
  312.         {
  313.                 Remove((struct Node *)node);
  314.                 FreeVec(node);
  315.                 node = nextnode;
  316.         }                        
  317. }
  318.         
  319. void CleanExit(void)
  320. {       CxMsg *msg;
  321.         
  322.         if (broker)
  323.            DeleteCxObjAll(broker); 
  324.              
  325.         if (cxport)
  326.         {   
  327.            while (msg = (CxMsg *)GetMsg(cxport))
  328.              ReplyMsg((struct Message *)msg);
  329.            RemPort(cxport);  
  330.            DeleteMsgPort(cxport);  
  331.         }   
  332.         
  333.         CloseAllWnd();
  334.         
  335.         if (! IsListEmpty(&myList))     
  336.             KillListView();
  337.             
  338.         CloseDownScreen();
  339.         
  340.         if (aguide)
  341.             CloseAmigaGuide(aguide);
  342.         
  343.         CloseLibs();
  344.             
  345.         if (IconBase)
  346.         {
  347.            ArgArrayDone();
  348.            CloseLibrary(IconBase);
  349.         }  
  350.             
  351.         if(rdargs)
  352.                 FreeArgs(rdargs); 
  353.         
  354.         exit(0);     
  355. }
  356.         
  357. BOOL OpenLibs(void)
  358. {
  359.         BOOL rc = FALSE;
  360.         
  361.           if (AmigaGuideBase = OpenLibrary("amigaguide.library", 39))
  362.             if (CxBase = OpenLibrary("commodities.library", 37))
  363.                 rc = TRUE;
  364.            
  365.         return rc;
  366.  
  367. void CloseLibs(void)
  368. {
  369.         if (LocaleBase)
  370.         {
  371.                 CloseCatalog(li.li_Catalog); 
  372.                 CloseLibrary(LocaleBase);
  373.         }
  374.         
  375.         if (AmigaGuideBase) CloseLibrary(AmigaGuideBase);
  376.         if (CxBase) CloseLibrary(CxBase);
  377. }     
  378.                  
  379.